package com.elinkway.infinitemovies.http.parser;

import android.text.TextUtils;

import com.elinkway.infinitemovies.bean.ChannelNewBean;
import com.elinkway.infinitemovies.bean.ChannelNewParams;
import com.elinkway.infinitemovies.bean.LivePlaytimeInfo;
import com.elinkway.infinitemovies.bean.LiveTvStationDetail;
import com.elinkway.infinitemovies.bean.RecommendBase;
import com.elinkway.infinitemovies.bean.RecommendData;
import com.elinkway.infinitemovies.bean.RecommendDataMainpage;
import com.elinkway.infinitemovies.utils.Utils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainPageRecommendParser extends
		LVideoMobileParser<RecommendDataMainpage> {
	
	private final String GALLERY_DTAT = "focusdata";
	
	private final String RECOMMEND_DATA = "recdatas";
	
	private final String LIVE_DATA="livedatas";

	private final String CHANNELS_DATA = "channels";

	@Override
	public RecommendDataMainpage parse(JSONObject data) throws Exception {
		if (null != data) {
			try {
				JSONObject dataObj = data.optJSONObject("data");
				if (null == dataObj) {
					return null;
				}
				RecommendDataMainpage mainPageData = new RecommendDataMainpage();

				JSONArray galleryArray = dataObj.optJSONArray(GALLERY_DTAT);
				if (null != galleryArray && galleryArray.length() > 0) {
					int galleryLength = galleryArray.length();
					ArrayList<RecommendBase> galleryList = new ArrayList<RecommendBase>();
					for (int i = 0; i < galleryLength; i++) {
						RecommendBase recBase = new RecommendBase();
						JSONObject obj = galleryArray.optJSONObject(i);
						getCommonRecommenData(obj, recBase);
						galleryList.add(recBase);
					}
					mainPageData.setmGralleryDatas(galleryList);
				}

				JSONArray recommendDataArray = dataObj.optJSONArray(RECOMMEND_DATA);
				if (null != recommendDataArray && recommendDataArray.length() > 0) {
					int recArrayLength = recommendDataArray.length();
					ArrayList<RecommendData> recommendLists = new ArrayList<RecommendData>();
					for (int i = 0; i < recArrayLength; i++) {
						JSONObject recommendObj = recommendDataArray
								.optJSONObject(i);
						RecommendData recData = new RecommendData();
						recData.setRecnum(recommendObj.optString("recnum"));
						recData.setRecname(recommendObj.optString("recname"));
						recData.setVt(recommendObj.optString("vt"));
						recData.setCategoryid(recommendObj.optString("categoryid"));
						recData.setAreaid(recommendObj.optString("areaid"));
						recData.setSort(recommendObj.optString("sort"));
						recData.setDisplay(recommendObj.optString("display"));
						//获取当前模块的展现方式
						recData.setPicsize(recommendObj.optString("picsize"));
						recData.setIsThrough(recommendObj.optString("is_through"));

						JSONArray recJSONArray = recommendObj.optJSONArray("data");
						if (null != recJSONArray && recJSONArray.length() > 0) {
							ArrayList<RecommendBase> recItemLists = new ArrayList<RecommendBase>();
							int length = recJSONArray.length();
							for (int j = 0; j < length; j++) {
								JSONObject recItemObj = recJSONArray.optJSONObject(j);
								RecommendBase recommendBase = new RecommendBase();
								getCommonRecommenData(recItemObj, recommendBase);
								recommendBase.setRating(recItemObj.optString("rating"));
								recommendBase.setIsend(recItemObj.optString("isend"));
								recommendBase.setEpisodes(recItemObj.optString("episodes"));
								recommendBase.setNowepisodes(recItemObj.optString("nowepisodes"));
								recItemLists.add(recommendBase);
							}
							recData.setRecommendDataLists(recItemLists);
						}
						recommendLists.add(recData);
					}
					mainPageData.setmRecommendDatas(recommendLists);
				}

				JSONArray channelsDataArray = dataObj.optJSONArray(CHANNELS_DATA);
				if (null != channelsDataArray && channelsDataArray.length() > 0) {
					int channelsArrayLength = channelsDataArray.length();
					List<ChannelNewBean> channelsLists = new ArrayList<ChannelNewBean>();
					ChannelNewBean channelBean = null;
					ChannelNewParams channelParams = null;
					for (int i = 0; i < channelsArrayLength; i++) {
						JSONObject channelObj = channelsDataArray.optJSONObject(i);
						if(channelObj != null){
							channelBean = new ChannelNewBean();
							channelBean.setId(channelObj.optString("id"));
							channelBean.setIcon(channelObj.optString("icon"));
							channelBean.setName(channelObj.optString("name"));
							channelBean.setType(channelObj.optString("type"));
							JSONObject paramObj = channelObj.optJSONObject("param");
							if(paramObj != null){
								channelParams = new ChannelNewParams();
								channelParams.setUrl(paramObj.optString("url"));
								channelParams.setTitle(paramObj.optString("title"));

								channelParams.setAid(paramObj.optString("aid"));
								channelParams.setVt(paramObj.optString("vt"));
								channelParams.setThemeId(paramObj.optString("theme_id"));
								channelParams.setSubcategory(paramObj.optString("subcategory"));
								channelParams.setArea(paramObj.optString("area"));
								channelParams.setYear(paramObj.optString("year"));
								channelParams.setOrderby(paramObj.optString("orderby"));

								channelBean.setParam(channelParams);
							}
							channelsLists.add(channelBean);
						}
					}
					mainPageData.setmChannelsList(channelsLists);
				}

				JSONArray liveDataArray = dataObj.optJSONArray(LIVE_DATA);
				if (null != liveDataArray && liveDataArray.length() > 0) {

					int channelSize = liveDataArray.length();
					ArrayList<LiveTvStationDetail> tvStationList = new ArrayList<LiveTvStationDetail>(channelSize);
					for (int j = 0; j < channelSize; j++) {
						LiveTvStationDetail liveStationDetail = new LiveTvStationDetail();
						JSONObject channelObj = liveDataArray.optJSONObject(j);

						JSONArray streams = channelObj.optJSONArray("streams");
						String url = channelObj.optString("url");

						if ((null != streams && streams.length() > 0) || !TextUtils.isEmpty(url)) {
							ArrayList<String> streamsList;
							int streamsSize;
							if (null != streams && streams.length() > 0) {
								streamsSize = streams.length();
								streamsList = new ArrayList<String>(streamsSize);

								for (int i = 0; i < streamsSize; i++) {
									String streamUrl = streams.optString(i);
									if (!TextUtils.isEmpty(streamUrl)) {
										streamsList.add(streamUrl);
									}
								}
							} else {
								streamsList = new ArrayList<String>();
							}

							if (!TextUtils.isEmpty(url)) {
								streamsList.add(url);
							}


							liveStationDetail.setStreams(streamsList);

							liveStationDetail.setId(channelObj.optString("id"));
							liveStationDetail.setIcon(channelObj.optString("icon"));
							liveStationDetail.setName(channelObj.optString("name"));

							JSONArray currentPlayArry = channelObj.optJSONArray("current");
							int currentPlaySize = 0;
							if (currentPlayArry != null) {
								currentPlaySize = currentPlayArry.length();
								ArrayList<LivePlaytimeInfo> playTimeList =
										new ArrayList<LivePlaytimeInfo>(currentPlaySize);
								for (int m = 0; m < currentPlaySize; m++) {
									LivePlaytimeInfo playTimeInfo = new LivePlaytimeInfo();
									JSONObject playTimeObj = currentPlayArry.optJSONObject(m);
									playTimeInfo.setPlayTime(playTimeObj.optString("playtime"));
									playTimeInfo.setTitle(playTimeObj.optString("title"));
									// 添加每次解析的播放时间信息
									playTimeList.add(playTimeInfo);
								}
								liveStationDetail.setPlayTimeList(playTimeList);
							}
							// 添加每个电视台的播放数据
							tvStationList.add(liveStationDetail);
						}
					}

					mainPageData.setmLiveDatas(tvStationList);
				}
				return mainPageData;
			}catch (Exception e){
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 获取通用的解析数据
	 * @param obj
	 * zhangshuo
	 * 2014年4月23日 下午3:45:12
	 */
    private void getCommonRecommenData(JSONObject obj, RecommendBase recBase) {
        recBase.setName(obj.optString("name"));
        recBase.setAid(obj.optString("aid"));
        recBase.setThemeid(obj.optString("themeid"));
        String playUrl = obj.optString("playurl");
        recBase.setPlayurl(playUrl);
        String display = obj.optString("display");
        recBase.setDisplay(display);
        recBase.setSubname(obj.optString("subname"));
        recBase.setPic(obj.optString("pic"));
        recBase.setPich(obj.optString("pich"));
        recBase.setVt(obj.optString("vt"));
        recBase.setShowtime(obj.optString("showtime"));
		recBase.setCornerColor(obj.optString("cornerColor"));
		recBase.setCornerTitle(obj.optString("cornerTitle"));
		recBase.setShareTitle(obj.optString("shareTitle"));
		recBase.setSharePic(obj.optString("sharePic"));
		recBase.setShareDesc(obj.optString("shareDesc"));

        if (Utils.LIVE.equals(display)) {
            JSONArray streams = obj.optJSONArray("streams");

            if ((null != streams && streams.length() > 0) || !TextUtils.isEmpty(playUrl)) {
                int streamsSize = streams == null ? 0 : streams.length();
                ArrayList<String> streamsList = new ArrayList<String>();

                if (!TextUtils.isEmpty(playUrl)) {
                    streamsList.add(playUrl);
                }

                for (int i = 0; i < streamsSize; i++) {
                    String streamUrl = streams.optString(i);
                    if (!TextUtils.isEmpty(streamUrl)) {
                        streamsList.add(streamUrl);
                    }
                }
                recBase.setStreams(streamsList);
            }
        }
    }
	
}
